$("#circle1,#circle2").addClass("anim-flip");
var socBefore = -1;
var b = 1;
var isInit = false;
$.ajax({
    url:"/cms/getWeatherByCityId?cityId=CN101121501",
    type:"get",
    success:function(data){
                        function weather() {
                    if (!isInit) {
                        var dataList = $.parseJSON(data);
                        $.each(dataList, function(i, item) {
                            var weather = $('.weather');
                            $('#weather').html(item.weather);
                            if (item.weather == "多云" || item.weather == "少云") {
                                weather.html("<img src='img/weathe_0001s_0001_cloudy.png'/>")
                            } else if (item.weather == "晴" || item.weather == "平静") {
                                weather.html("<img src='img/weathe_0002s_0000_clear-day.png'/>")
                            } else if (item.weather == "有风" || item.weather == "微风" || item.weather == "和风" || item.weather == "清风" || item.weather == "强风/劲风" || item.weather == "疾风" || item.weather == "大风" || item.weather == "烈风") {
                                weather.html("<img src='img/weathe_0001s_0003_wind.png'/>")
                            } else if (item.weather == "阵雨" || item.weather == "强阵雨" || item.weather == "雷阵雨" || item.weather == "强雷阵雨" || item.weather == "雷阵雨伴有冰雹" || item.weather == "小雨" || item.weather == "中雨" || item.weather == "大雨" || item.weather == "暴雨" || item.weather == "大暴雨" || item.weather == "特大暴雨") {
                                weather.html("<img src='img/weathe_0000s_0000_rain.png'/>")
                            } else if (item.weather == "小雪" || item.weather == "中雪" || item.weather == "大雪" || item.weather == "雨夹雪") {
                                weather.html("<img src='img/weathe_0000s_0001_snow.png'/>")
                            } else if (item.weather == "晴间多云" || item.weather == "阴") {
                                weather.html("<img src='img/weathe_0002s_0002_partly-cloudy-day.png'/>")
                            }
                            $('#city_temperature').html(item.city_temperature);
                            $('#pm_level').html(item.pm_level);
                            $('#pm25').html(item.pm25);
                            if (item.pm25 <= 35) {
                                $('.pm').css('background', '#84df33');
                            } else if (item.pm25 > 35 && item.pm25 <= 53) {
                                $('.pm').css('background', '#f09e4e');
                            } else if (item.pm25 > 54 && item.pm25 <= 70) {
                                $('.pm').css('background', '#f17c73');
                            } else if (item.pm25 > 70) {
                                $('.pm').css('background', '#ce30ff');
                            }

                        });
                    } else {
                        isInit = true;
                    }

                }

                weather();
    }
})
function chargingStation() {
    $.ajax({
        url: "/cms/getBigScreenByStationId" + location.search,
        type: "get",
        success: function(data) {
            var dataList = $.parseJSON(data);
            if (dataList.pojo.directs.length == 0 && dataList.pojo.alternates.length == 0) {
                swal({
                        title: "查询的桩ID不存在，请重新查询！", 
                    }, function() {
                        return window.location = "search.html";
                    }); 
            } else {
                function formatId(id) {
                    if (!!id) {
                        return id.replace(/(\w{3}).*(\w{4})/, "$1****$2");
                    } else {
                        return '';
                    }
                };

                function ACchargeSpots() {
                    var dataList = $.parseJSON(data);
                    var alternates = dataList.pojo.alternates;
                    var num = 0;
                    if (dataList.pojo !== null) {
                        $.each(alternates, function(n, alter) {
                            var alterCard = alter.user_pay_card;
                            var num = $('#exchange' + n);
                            if (alter.charge_status == 0) {

                                num.html('<div class="free-div"><img src="img/free.png"></div><div class="chargestate" style="margin-top:20px">空闲中</div>');
                                $('#alternating_state').html('有空闲');
                                $('#alternating_state').css('background-color', '#a8ce34');


                            } else if (alter.charge_status == 1) {

                                num.html('<div class="dcState1"><div class="dcState2" ><img src="img/01.png" /><img class="user" src="img/userS.png" /></div><div class="chargestate" id="AC_card" style="margin-top:20px">已被<span>' + formatId(alterCard) + '</span>用户预约</div></div>');


                            } else if (alter.charge_status == 2) {

                                num.html('<div class="charging"><div id=""><img src="img/charging.png" /></div><img class="battery" name="battery" src="img/batteryM1.png" /></div><div class="chargestate" id="cha">用户' + formatId(alterCard) + '充电中...</div>');
                            }

                        });
                    } else if (dataList.pojo == null) {
                        var DP = $('.display');
                        DP.css('display', 'block');
                    }

                };

                function DCchargeSpots() {

                    var DCPileStatusMap = new Object();
                    DCPileStatusMap.DCPileStatusList = new Object();
                    DCPileStatusMap.length = 0;
                    var dataList = $.parseJSON(data);
                    $.each(dataList.pojo.directs, function(i, item) {
                        var DCPileStatus = DCPileStatusMap.DCPileStatusList[item.charger_id];
                        if (!!!DCPileStatus) {
                            DCPileStatus = new Object();
                            DCPileStatus.portList = new Array();
                            DCPileStatusMap.length += 1;
                        }
                        DCPileStatus.charger_id = item.charger_id;

                        DCPileStatus.portList.push({
                            port_no: item.port_no,
                            charge_status: item.charge_status,
                            soc: !!item.soc ? item.soc : 0,
                            user_pay_card: item.user_pay_card
                        });
                        DCPileStatusMap.DCPileStatusList[item.charger_id] = DCPileStatus;
                    });
                    $.each(dataList.pojo.directs, function(i, item) {
                        var DCPileStatus = DCPileStatusMap.DCPileStatusList[item.charger_id];
                        if (!!!DCPileStatus) {
                            DCPileStatus = new Object();
                            DCPileStatus.portList = new Array();
                            DCPileStatusMap.length += 1;
                        }
                        DCPileStatus.charger_id = item.charger_id;

                        DCPileStatus.portList.push({
                            port_no: item.port_no,
                            charge_status: item.charge_status,
                            soc: !!item.soc ? item.soc : 0,
                            user_pay_card: item.user_pay_card
                        });
                        DCPileStatusMap.DCPileStatusList[item.charger_id] = DCPileStatus;
                    });
                    var count = 0;
                    $.each(DCPileStatusMap.DCPileStatusList, function(i, DCPileStatus) {

                        if (count > 0) {
                            return;
                        }
                        $.each(DCPileStatus.portList, function(t, item) {
                            var card = formatId(item.user_pay_card);
                            var dcState = $('#port' + (t + 1));
                            var gun = $('#gun' + (t + 1));
                            var soc = item.soc;

                            if (soc >= 100) {
                                soc = 100;
                            }
                            if (soc == 0) {
                                soc = 30;
                            }
                            if (-1 == socBefore) {
                                socBefore = soc;
                            }
                            if (item.charge_status == 0) {
                                dcState.html("<div class='free-div'><img src='img/free.png'></div><div class='chargestate'>空闲中...</div>");
                                $('#direct_state').html('有空闲');
                                $('#direct_state').css('background-color', '#a8ce34');
                                gun.css('background-image', 'url(./img/trianglegreen.png )');
                            } else if (item.charge_status == 1) {
                                dcState.html('<div class="dcState2" ><img src="img/01.png" /><img class="user" src="img/userS.png" /></div><div class="chargestate">已被<span>' + card + '</span>用户预约</div>');
                                gun.css('background-image', 'url(./img/triangle.png)');

                            } else if (item.charge_status == 2) {
                                roundCharging();

                                function roundCharging() {


                                    if (soc == 100) {
                                        gun.css('background-image', 'url(./img/triangleblue.png)');
                                        dcState.html('<div class="bigcircle"><div class="circle anim-flip show" id="round' + t + '"><img class="bgcircle" src="img/circle1.png" /><img class="battery"  src="img/batteryS4.png" /> <strong></strong></div><div id="HiddenSmall' + t + '" class="hiddencirclesmall"></div></div><div class="chargestate" id="charging">用户' + card + '已充满...</div>');
                                    } else {
                                        gun.css('background-image', 'url(./img/triangleblue.png)');
                                        dcState.html('<div class="bigcircle"><div class="circle anim-flip show" id="round' + t + '"><img class="bgcircle" src="img/circle1.png" /><img class="battery" name="battery" src="img/batteryS1.png" /> <strong></strong></div><div id="HiddenSmall' + t + '" class="hiddencirclesmall"></div></div><div class="chargestate" id="charging">用户' + card + '充电中...</div>');
                                    }
                                    var round = $('#round' + t);
                                    var round_small = $('#HiddenSmall' + t);
                                    round.circleProgress({
                                        value: soc / 100,
                                        startAngle: -1.5,
                                        size: 468,
                                        animationStartValue: socBefore / 100,
                                        emptyFill: 'rgba(0, 0, 0, .3)',
                                    }).on('circle-animation-progress', function(event, progress) {
                                        $(this).find('strong').html(parseInt(soc) + '%');
                                    });
                                    round_small.circleProgress({
                                        value: soc / 100,
                                        startAngle: -1.5,
                                        size: 520,
                                        animationStartValue: socBefore / 100,
                                        emptyFill: '#343738',
                                        fill: {
                                            color: 'rgba(255, 255, 255, .0)'
                                        }
                                    });
                                    socBefore = soc;

                                };



                            }

                        });
                        count++;
                    })
                }

                ACchargeSpots();
                DCchargeSpots();





            }

        },
        error: function(responseText, textStatus, XMLHttpRequest) {
            var DP = $('.display');
            DP.css('display', 'block');
            $('#circle1').circleProgress({
                value: 80 / 100,
                startAngle: -1.5,
                size: 468,
                emptyFill: 'rgba(0, 0, 0, .1)',
            }).on('circle-animation-progress', function(event, progress) {
                $(this).find('strong').html(parseInt(80 * progress) + '%');
            });
            $("#circleHiddenSmall").circleProgress({
                value: 80 / 100,
                startAngle: -1.5,
                size: 520,
                animationStartValue: 0,

                emptyFill: '#343738',
                fill: {
                    color: 'rgba(255, 255, 255, .0)'
                }
            }).on('circle-animation-progress', function(event, progress) {
                $(this).find('strong').html(parseInt(80 * progress) + '%');
            });
        }

    });

};
// setTimeout(chargingStation,20*1000);
setInterval(chargingStation,60*1000);
// var count = 0;
// setInterval(function() {
//     count++;
//     if (count == 1 || count % 3 == 0) {
//         chargingStation();
//     }
// }, 20 * 1000);
chargingStation();